/**
 * Created by Administrator on 2019/1/31.
 */
$(function () {
    clearTheForm();// 清空表单
    addNew();// 保存信息
    refreshNo();// 刷新编号
    //choosePic();//选择图片
});
// 保存模具信息
var addNew = function () {
    $("#save").click(function () {
        if($("input[name='noticeNo']").val()=="") {
            showDialogWithContent("请输入公告编号！");
            return;
        } else if($("input[name='noticeTitle']").val()==""){
            showDialogWithContent("请输入公告标题！");
            return;
        } else if($("input[name='noticeContent']").val()==""){
            showDialogWithContent("请输入公告内容！");
            return;
        }
        var params = new Object();
        params.noticeNo = $("input[name='noticeNo']").val();
        params.noticeTitle = $("input[name='noticeTitle']").val();
        params.noticeContent = $("input[name='noticeContent']").val();
        params.remark = $("input[name='remark']").val();
        console.log($("input[name='isShow']").prop('checked'));
        if($("input[name='isShow']").prop('checked') == true) {
            params.isShow = 1;
        } else {
            params.isShow = 0;
        }
        params.picAddr = $("input[name='picAddr']").val();
        var jsonstr = postSyncAjax("/notice/addNew", params);
        if(jsonstr.success) {
            $("#viewPicDiv").hide();
            $("#pic").val("");
            $("#viewPic").attr("src","");
            $("#picAddr").val("");
            showDialogWithContent("添加成功！");
        } else {
            if (jsonstr.status == 401) {
                showDialogSetTime(jsonstr.msg, function () {
                    window.top.location.href = "/index";
                }, 5000);
            }
            $("#viewPicDiv").hide();
            $("#viewPic").attr("src","");
            $("#picAddr").val("");
            showDialogWithContent("添加失败：" + jsonstr.msg);
            return;
        }
        /*// 用于ajax上传文件和普通数据
         var formData = new FormData(document.getElementById("addMouldForm"));
         console.log(formData);
         var jsonstr = postIncludeFileSyncAjax("/addNewMould",formData);
         console.log(jsonstr);
         if(jsonstr.status == 300) {
         showDialogWithContent("添加成功！");
         } else {
         if(jsonstr.status == 401){
         showDialogSetTime(jsonstr.msg,function () {
         window.top.location.href = "/index";
         },5000);
         }
         showDialogWithContent("添加失败："+jsonstr.msg);
         return;
         }*/
    });
}
// 刷新模具编号
var refreshNo = function () {
    $("#refresh").click(function () {
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        var hour = date.getHours();
        var minute = date.getMinutes();
        var seconds= date.getSeconds();
        month = month < 10 ? '0' + month : month;
        day = day < 10 ? '0' + day : day;
        hour = hour < 10 ? '0' + hour : hour;
        minute = minute < 10 ? '0' + minute : minute;
        seconds = seconds < 10 ? '0' + seconds : seconds;
        $("#noticeNo").textbox("setValue","NT" + year + "" + month + "" + day + "" + hour + "" + minute + "" + seconds);
    });
}
// 清空表单
var clearTheForm = function () {
    $("#clear").click(function () {
        $('#addNoticeForm').form('clear');
    });
}
// 选择图片
var choosePic = function () {
    $(document).off('change','#pic').on('change','#pic',function () {
        if($(this).val()!=null && $(this).val()!=""){
            console.log($(this).val());
            var params = new Object();
            params.picAddr = $("#picAddr").val();
            $.ajaxFileUpload({
                url: "/uploadTempMouldPic",   //用于文件上传的服务器端请求地址
                secureuri: false,      //是否需要安全协议，一般设置为false
                fileElementId: ['pic'], //文件上传域的ID
                dataType: 'json',           //返回值类型 一般设置为json
                data:params,
                type:'post',
                success: function (data, status)  //服务器成功响应处理函数
                {
                    console.log(data);
                    console.info(status);
                    if(data.success == true) {
                        $("#viewPicDiv").show();
                        $("#viewPic").attr("src",data.data);
                        $("#picAddr").val(data.data);
                        console.log($("#picAddr").val());
                    } else {
                        if(data.status == 401){
                            showDialogSetTime(data.msg,function () {
                                window.top.location.href = "/index";
                            },5000);
                        }
                        $("#viewPicDiv").hide();
                        $("#viewPic").attr("src","");
                        $("#picAddr").val();
                        return;
                    }
                },
                error: function (data, status, e)//服务器响应失败处理函数
                {
                    $("#viewPicDiv").hide();
                    $("#viewPic").attr("src","");
                    $("#picAddr").val();
                    showDialogWithContent("上传过程出错，错误信息："+e);
                }
            });
        } else {
            $("#viewPicDiv").hide();
            $("#viewPic").attr("src","");
            $("#picAddr").val();
        }
    });
}